/*
 * @(#)PlayerManager.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.model.player;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.netexplode.jtunes.model.media.CollectionManager;
import com.netexplode.jtunes.model.media.MediaFile;

/**
 * <code>PlayerManager</code> represents .....
 * 
 * @author ykim
 * @version $Revision: 1.3 $
 * @since 0.1
 */
public class PlayerManager {

    private static final Log log = LogFactory.getLog(PlayerManager.class);

    private boolean playing;

    private boolean initialized;

    private static PlayerManager instance = null;

    /**
     * @return
     */
    public static PlayerManager getInstance() {
        if (instance == null) {
            throw new RuntimeException("PlayerManager is not yet initialized");
        }

        return instance;
    }

    public static PlayerManager getInstance(String[] names, String[] locations)
            throws IOException {
        if (instance == null) {
            synchronized (PlayerManager.class) {
                instance = new PlayerManager(names, locations);
                instance.setInitialized(true);
            }
        }

        return instance;
    }

    /**
     * Creates an instance of Jukebox with multiple root location with a given
     * name
     * 
     * @param locations
     */
    private PlayerManager(String[] names, String[] locations)
            throws IOException {
        // @todo add validation
        for (int i = 0; i < names.length; i++) {
            log.info(names[i] + " : adding collection from " + locations[i]);
            CollectionManager.getInstance().getLocalCollections().add(
                    CollectionManager.getInstance().newInstance(
                            names[i],
                            locations[i]));
        }
    }

    /**
     * @return Returns the mediaPlayer.
     */
    public MediaPlayer getMediaPlayer() {

        MediaPlayer mediaPlayer = null;
        final MediaFile mediaFile = CollectionManager
                .getInstance()
                .getCurrentMediaFile();

        if (mediaFile.getFormat().equalsIgnoreCase("mp3")) {
            mediaPlayer = new Mp3Player();
        }
        else {
            mediaPlayer = new Mp4Player();
            // @todo
        }

        log.info("mediaPlayer = " + mediaPlayer);

        return mediaPlayer;
    }

    /**
     * @return Returns the playing.
     */
    public boolean isPlaying() {
        return this.playing;
    }

    /**
     * @param playing
     *            The playing to set.
     */
    public void setPlaying(boolean playing) {
        this.playing = playing;
    }

    /**
     * @return Returns the initialized.
     */
    public boolean isInitialized() {
        return this.initialized;
    }

    /**
     * @param initialized
     *            The initialized to set.
     */
    public void setInitialized(boolean initialized) {
        this.initialized = initialized;
    }

}